﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using XT.BusinessService;
using XT.Model;
using XT.Web.External;
namespace XT.Web.Controllers
{
    public class RoomController : Controller
    {
        //
        // GET: /Room/

        public ActionResult Index(int id)
        {
            var roomService = MvcApplication.GetInstance<IRoomService>();
            var accountService = MvcApplication.GetInstance<IAccountService>();
            Room room = (Room)roomService.getRoomById(id);            
            var amentityService = MvcApplication.GetInstance<IAmentityService>();
            IEnumerable<Amentity> amentities = amentityService.GetAmentities();
         
            //Account acc = accountService.getAccountById(Int32.Parse(Request.Cookies["Id"].Value));
            Account acc = accountService.getAccountById(1);
            return View(Tuple.Create(room, amentities,acc));
           
        }

        public ActionResult Photos(int id)
        {
            var roomService = MvcApplication.GetInstance<IRoomService>();
            Room room = (Room)roomService.getRoomById(id);
            return View(room);
        }

 

        public ActionResult GetRoomReviews(int idRoom)
        {
            
            var roomreviewService = MvcApplication.GetInstance<IRoomReviewService>();
            List<Room_Review> reviews = roomreviewService.getReviews(idRoom).ToList();

            return PartialView("RoomReviews",reviews);
        }

        public ActionResult GetObjects(int roomId)
        {
            //get amentities
            var roomService = MvcApplication.GetInstance<IRoomService>();
            Room room = roomService.getRoomById(roomId);
            XElement xmlAmentities = room.Room_Amentity;
            List<XElement> amentities = (from l in xmlAmentities.Elements("item")
                                       select l).ToList();
            List<Models.Amentity> manyAmentities = new List<Models.Amentity>();
            foreach (XElement item in amentities)
            {
                Models.Amentity amentity = new Models.Amentity();
                amentity.id = item.Attribute("id").Value;
                amentity.isTrue = item.Value;
                manyAmentities.Add(amentity);
            }
            // get utilities
            var utilityService = MvcApplication.GetInstance<IUtilityPlaceService>();
            IEnumerable<Utility_Place> _utilities = utilityService.getUtilitysByArea(room.Area_Id);
            List<Models.UtilityPlace> utilities = new List<Models.UtilityPlace>();
            foreach (Utility_Place utility in _utilities)
            {
                Models.UtilityPlace _utility = new Models.UtilityPlace();
                _utility.id = utility.Id;
                _utility.name = utility.Utility_Place_Name;
                _utility.latitude = utility.Utility_Place_Latitude;
                _utility.longitude = utility.Utility_Place_Longitude;
                utilities.Add(_utility);
            }
            List<Models.UtilityPlace> nearestUtilities = Helper.FindTheNearestUtilityPlaces(room,utilities,1,2);
            return Json(new { amentities = manyAmentities, latitude = room.Room_Latitude, longitude = room.Room_Longitude, utilities = nearestUtilities });
        }

        public ActionResult UtilityView(Models.UtilityPlace utility)
        {
            return Json(new { content = Helper.RenderViewToString(this.ControllerContext, "UtilityPlace", utility) });
        }

        public ActionResult AddRoomReview(FormCollection col)
        {
            var roomreviewService = MvcApplication.GetInstance<IRoomReviewService>();
            string title = col["cmtTitle"];
            string content = col["cmtContent"];
            int account = int.Parse(col["cmtAcc"]);
            int room = int.Parse(col["cmtId"]);
            Room_Review rv = new Room_Review
            {
                Account_Id = account,
                Room_Id = room,
                Review_Title = title,
                Review_Content = content
            };
           
            if ((roomreviewService.AddRoomReview(rv) != 1))
            {
                return Json(new { success=false});
            }
            return Json(new { success = true });
        }
    }
}
